package com.example.hxds.mps.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.hxds.common.exception.HxdsException;
import com.example.hxds.mps.service.MapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;

/**
 * @ClassName: MapServiceImpl
 * @Description: 地图模块服务实现
 * @Author: 丛雨
 * @Date: 2024/1/25 20:34
 */
@Service
@Slf4j
public class MapServiceImpl implements MapService {
    @Value("${tencent.map.key}")
    private String key;

    private final static String distanceUrl = "https://apis.map.qq.com/ws/distance/v1/matrix";
    private final static String directionUrl = "https://apis.map.qq.com/ws/direction/v1/driving";

    @Override
    public HashMap estimateOrderMileageAndMinute(String mode, String startPlaceLatitude, String startPlaceLongitude, String endPlaceLatitude, String endPlaceLongitude) {
        // 创建HTTP请求并传入表单参数
        HttpRequest request = new HttpRequest(distanceUrl);
        request.form("mode", mode);
        request.form("from", startPlaceLatitude + "," + startPlaceLongitude);
        request.form("to", endPlaceLatitude + "," + endPlaceLongitude);
        request.form("key", key);
        HttpResponse response = request.execute();
        // 将返回的响应转换为JSON
        JSONObject json = JSONUtil.parseObj(response.body());

        int status = json.getInt("status");
        String message = json.getStr("message");
        if (status != 0) {
            log.error(message);
            throw new HxdsException("预估里程异常 : " + message);
        }

        // 得到距离计算的数组
        JSONArray rows = json.getJSONObject("result").getJSONArray("rows");
        JSONObject element = rows.get(0, JSONObject.class).getJSONArray("elements").get(0, JSONObject.class);
        // 起点到终点的距离,单位:米
        int distance = element.getInt("distance");
        String mileage = new BigDecimal(distance).divide(new BigDecimal(1000)).toString();
        // 起点到终点结合路况的时间,单位:秒
        int duration = element.getInt("duration");
        String minute = new BigDecimal(duration).divide(new BigDecimal(60), 0, RoundingMode.CEILING).toString();
        HashMap map = new HashMap() {{
            put("mileage", mileage);
            put("minute", minute);

        }};
        return map;
    }

    @Override
    public HashMap calculateDriveLine(String startPlaceLatitude, String startPlaceLongitude, String endPlaceLatitude, String endPlaceLongitude) {
        // 创建HTTP请求并传入表单参数
        HttpRequest request = new HttpRequest(directionUrl);
        request.form("from", startPlaceLatitude + "," + startPlaceLongitude);
        request.form("to", endPlaceLatitude + "," + endPlaceLongitude);
        request.form("key", key);
        HttpResponse response = request.execute();
        System.out.println(response);
        // 将返回的响应转换为JSON
        JSONObject json = JSONUtil.parseObj(response.body());

        int status = json.getInt("status");
        String message = json.getStr("message");
        if (status != 0) {
            log.error(message);
            throw new HxdsException("线路规划异常 : " + message);
        }
        // 得到线路规划结果
        JSONObject result = json.getJSONObject("result");
        HashMap map = result.toBean(HashMap.class);
        return map;
    }
}
